package cn.ultrarisk.scadmin.manager.biz.solr.factory;

import cn.ultrarisk.scadmin.manager.biz.solr.dao.SolrNodeDAO;
import cn.ultrarisk.scadmin.manager.biz.solr.dao.SolrReplicaDAO;
import cn.ultrarisk.scadmin.manager.biz.solr.entity.SolrNode;
import cn.ultrarisk.scadmin.manager.biz.solr.entity.SolrReplica;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * HttpSolrServerFactory
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/6/17
 * @since V1.0
 */
@Component
public class HttpSolrServerFactory {
    @Autowired
    private SolrReplicaDAO solrReplicaDAO;
    @Autowired
    private SolrNodeDAO solrNodeDAO;

    public HttpSolrServer getNodeServer(Integer replicaId) {
        SolrReplica replica = solrReplicaDAO.get(replicaId, SolrReplica.CORE_NAME, SolrReplica.NODE_ID);
        SolrNode node = solrNodeDAO.get(replica.getNodeId(), SolrNode.HOST, SolrNode.PORT, SolrNode.PATH);
        return new HttpSolrServer(node.getBaseUrl());
    }

    public HttpSolrServer getCoreServer(Integer replicaId) {
        SolrReplica replica = solrReplicaDAO.get(replicaId, SolrReplica.CORE_NAME, SolrReplica.NODE_ID);
        SolrNode node = solrNodeDAO.get(replica.getNodeId(), SolrNode.HOST, SolrNode.PORT, SolrNode.PATH);
        return new HttpSolrServer(node.getBaseUrl() + "/" + replica.getCoreName());
    }
}
